//
//  HJDKnowledgeViewController.m
//  常州红房子
//
//  Created by 胡金东 on 16/7/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDKnowledgeViewController.h"
#import "HJDDietViewController.h"
#import "HJDEducationViewController.h"
#import "HJDParentingViewController.h"
#import "HJDPaKnowledgeViewController.h"
#import "HJDTitleLabel.h"
#import "BSMacros.h"


//#import "XMGSocialVirewController.h"
@interface HJDKnowledgeViewController ()<UIScrollViewDelegate>
{
    int x;
}

@property (nonatomic,strong) UIScrollView * titleScrollView;
@property (nonatomic,strong) UIScrollView * contentScrollView;



@end

@implementation HJDKnowledgeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getColorWithHexNumber:@"FBFBFB"];
    [self setUpTitleScrollView];
    [self setUpContentScrollView];
    
    // 添加子控制器
//    [self setUpChildVC];
    
    // 添加标题
    [self setUpTitle];
    // 默认显示第0个子控制器
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
    
}

-(void)setUpTitleScrollView
{
    
    self.titleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,HJDWidth, 70)];

    self.titleScrollView.backgroundColor = [UIColor clearColor];
//    self.titleScrollView.delegate = self;
//    self.contentScrollView.pagingEnabled = YES;
    
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.showsVerticalScrollIndicator = NO;
//
    [self.view addSubview:self.titleScrollView];
}

-(void)setUpContentScrollView
{
    self.contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, HJDWidth, HJDHeight)];

    self.contentScrollView.delegate = self;
    self.contentScrollView.pagingEnabled = YES;
    
    //    水平滚动条不显示
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = YES;
    //不让水平拖动
    self.contentScrollView.alwaysBounceHorizontal = NO;
    
    [self.view addSubview:self.contentScrollView];

}

-(void)setUpChildVC
{
   
    HJDDietViewController *diet = [[HJDDietViewController alloc]init];
//    diet.title = @"国际";
    [self addChildViewController:diet];
    

//    HJDEducationViewController *edu = [[HJDEducationViewController alloc]init];
//    edu.title = @"育儿";
//    [self addChildViewController:edu];
//    HJDParentingViewController *parent = [[HJDParentingViewController alloc]init];
//    parent.title = @"孕期知识";
//    [self addChildViewController:parent];
//    HJDPaKnowledgeViewController *pak = [[HJDPaKnowledgeViewController alloc]init];
//    pak.title = @"饮食注意";
//    [self addChildViewController:pak];
    
    
}

-(void)setUpTitle
{
    
    CGFloat lblY = 20;
    CGFloat lblH = 40;
    CGFloat lblW = 100;
    
        NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
        NSMutableArray *typeArr = [defa objectForKey:@"TypeArr"];
    for (int i = 0; i<typeArr.count; i++) {
        [self setUpChildVC];
        
        HJDTitleLabel *label = [[HJDTitleLabel alloc]init];
//        label.text = [self.childViewControllers[i] title];
        NSLog(@"self==%@",self.childViewControllers);
        label.text = typeArr[i];
        label.font = [UIFont systemFontOfSize:19];
        CGFloat lblX = i*lblW;
        label.frame = CGRectMake(lblX, lblY, lblW, lblH);
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)] ];

        label.tag = i;
        [self.titleScrollView addSubview:label];
        if (i==0) {
            label.scale = 1.0;
        }
    }
    int type = (int)typeArr.count;
    NSLog(@"type%d",type);

    self.titleScrollView.contentSize = CGSizeMake(type * lblW, 0);
    self.contentScrollView.contentSize = CGSizeMake(type * [UIScreen mainScreen].bounds.size.width, 0);
}

-(void)labelClick:(UITapGestureRecognizer *)tap
{
    // 取出被点击label的索引
    NSInteger index = tap.view.tag;
    NSUserDefaults *defaIndx = [NSUserDefaults standardUserDefaults];
    [defaIndx setObject:[NSString stringWithFormat:@"%ld",(long)index] forKey:@"index"];
    
    // 让底部的内容scrollView滚动到对应位置
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = index * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 一些临时变量
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;
    NSUserDefaults *defaIndx = [NSUserDefaults standardUserDefaults];
    [defaIndx setObject:[NSString stringWithFormat:@"%ld",(long)index] forKey:@"index"];
    
    // 让对应的顶部标题居中显示
    HJDTitleLabel *label = self.titleScrollView.subviews[index];
    CGPoint titleOffset = self.titleScrollView.contentOffset;
    titleOffset.x = label.center.x - width * 0.5;
    // 左边超出处理
    if (titleOffset.x < 0) titleOffset.x = 0;
    // 右边超出处理
    CGFloat maxTitleOffsetX = self.titleScrollView.contentSize.width - width;
    if (titleOffset.x > maxTitleOffsetX) titleOffset.x = maxTitleOffsetX;
    
    [self.titleScrollView setContentOffset:titleOffset animated:YES];

   
   
    
    // 让其他label回到最初的状态
    for (HJDTitleLabel *otherLabel in self.titleScrollView.subviews) {
        if (otherLabel != label) otherLabel.scale = 0.0;
    }
    
    // 取出需要显示的控制器
    UIViewController *willShowVc = self.childViewControllers[index];
    
    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;
    
    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}

/**
 * 手指松开scrollView后，scrollView停止减速完毕就会调用这个
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/**
 * 只要scrollView在滚动，就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scale = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (scale < 0 || scale > self.titleScrollView.subviews.count - 1) return;
    
    // 获得需要操作的左边label
    NSInteger leftIndex = scale;
    HJDTitleLabel *leftLabel = self.titleScrollView.subviews[leftIndex];
    
    // 获得需要操作的右边label
    NSInteger rightIndex = leftIndex + 1;
    HJDTitleLabel *rightLabel = (rightIndex == self.titleScrollView.subviews.count) ? nil : self.titleScrollView.subviews[rightIndex];
    
    // 右边比例
    CGFloat rightScale = scale - leftIndex;
    // 左边比例
    CGFloat leftScale = 1 - rightScale;
    
    // 设置label的比例
    leftLabel.scale = leftScale;
    rightLabel.scale = rightScale;
}


@end
